import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path";
import VueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig(({mode}) =>{
  // 加载环境配置
  const env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue(),
      VueDevTools(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    resolve:{
      alias:{
        '@': path.resolve(__dirname, './src'), // 将 @ 映射到 src 目录
        '$': path.resolve(__dirname, './public')
      }
    },
    server: {
      port: 7000,
      proxy: {
        '/api': {
          target: env.VITE_API_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
          pathRewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
})
